"""PAMR False Discovery Rate

.. helpdoc::
Calculates the false discovery rate of a set of genes from a PAMR fit.  This can be useful in conjunction with the cross validations to determine the optimal threshold for PAM analysis.

"""


"""<widgetXML>
    <name>PAMR False Discovery Rate</name>
    <icon></icon>
    <tags>
        <tag>PAMR</tag>
    </tags>
    <summary>Generate a false discovery rate predictor for a PAMR trained mode.</summary>
    <author>
        <authorname>Red-R Core Development Team</authorname>
        <authorcontact>www.red-r.org</authorcontact>
    </author>
    </widgetXML>
"""

"""
<name>PAMR FDR</name>
<author>Generated using Widget Maker written by Kyle R. Covington</author>
<description>Calculates the false discovery rate of a set of genes from a PAMR fit.  This can be useful in conjunction with the cross validations to determine the optimal threshold for PAM analysis.</description>
<RFunctions>pamr:pamr.fdr</RFunctions>
<tags>PAMR</tags>
<icon></icon>
<inputs>Trained PAMR Fit, PAMR Data Collection</inputs>
<outputs>PAMR FDR Model Fit</outputs>
"""
from OWRpy import * 
import redRGUI, signals


class RedRpamr_fdr(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        self.setRvariableNames(["pamr.fdr"])
        self.require_librarys(["pamr"])
        self.data = {}
        self.RFunctionParam_trained_obj = ''
        self.RFunctionParam_data = ''
        
        """.. rrsignals::"""
        self.inputs.addInput("trained_obj", "Trained PAMR Fit", signals.pamr.RPAMRFit, self.processtrained_obj)
        
        """.. rrsignals::"""
        self.inputs.addInput("data", "PAMR Data Collection (optional)", signals.pamr.RPAMRData, self.processdata)
        
        """.. rrsignals::"""
        self.outputs.addOutput("pamr.fdr Output", "PAMR FDR Model Fit", signals.pamr.RPAMRFDRFit)
        
        self.RFunctionParamnperms_lineEdit = redRGUI.base.lineEdit(self.controlArea, label = "nperms:", text = '100')
        redRGUI.base.commitButton(self.bottomAreaRight, "Commit", callback = self.commitFunction)
    def processtrained_obj(self, data):
        
        if data:
            self.RFunctionParam_trained_obj=str(data.getData())
            self.RFunctionParam_data = str(data.getOptionalData('parent')['data'])
            #self.data = data
            self.commitFunction()
        else:
            self.RFunctionParam_trained_obj=''
    def processdata(self, data):
        
        if data:
            self.RFunctionParam_data=str(data.getData())
            #self.data = data
            self.commitFunction()
        else:
            self.RFunctionParam_data=''
    def commitFunction(self):
        if unicode(self.RFunctionParam_trained_obj) == '': return
        if unicode(self.RFunctionParam_data) == '': return
        injection = []
        if unicode(self.RFunctionParamnperms_lineEdit.text()) != '':
            string = 'nperms='+unicode(self.RFunctionParamnperms_lineEdit.text())+''
            injection.append(string)
        inj = ','.join(injection)
        self.R(self.Rvariables['pamr.fdr']+'<-pamr.fdr(trained.obj='+unicode(self.RFunctionParam_trained_obj)+',data='+unicode(self.RFunctionParam_data)+','+inj+')')
        newData = signals.pamr.RPAMRFDRFit(self, data = self.Rvariables["pamr.fdr"]) # moment of variable creation, no preexisting data set.  To pass forward the data that was received in the input uncomment the next line.
        #newData.copyAllOptinoalData(self.data)  ## note, if you plan to uncomment this please uncomment the call to set self.data in the process statemtn of the data whose attributes you plan to send forward.
        self.rSend("pamr.fdr Output", newData)
